package com.weilus.clients;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.weilus.refresh.CilentsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.Collections;
import java.util.Map;

@Configuration
public class OauthService {

    @Autowired
    WebClient.Builder builder;
    @Autowired
    CilentsConfig config;

    @HystrixCommand(fallbackMethod = "checkTokenFallback")
    public Map checkToken(String token){
        return builder.build()
                .post()
                .uri(config.getCheckToken())
                .body(Flux.just(token),String.class)
                .retrieve()
                .bodyToMono(Map.class)
                .block(Duration.ofSeconds(30));
    }

    public Map checkTokenFallback(String token) {
        return Collections.singletonMap("error","oauth认证中心不可用!");
    }

}
